package chapter07

import scala.collection.mutable.ArrayBuffer

object Test02_ArrayBuffer {
  def main(args: Array[String]): Unit = {
    //1. 创建可变数组
    val arr1 = new ArrayBuffer[Int](5)
    val arr2 = ArrayBuffer(23, 57, 95)
    println(arr1)
    println(arr2.mkString(","))
    //2.访问元素
    //   println(arr1(0))
    arr2(1) = 65
    println(arr2(1))
    //3.添加元素
    val newArr1 = arr1 :+ 12
    println(newArr1)
    println(arr1)
    println(newArr1 == arr1)
    val newArr2 = arr1 += 19
    println(newArr2)
    println(arr1)
    println(arr1 == newArr2)
    //头部添加
    19 +=: arr1
    println(arr1)
    arr1.append(36)
    arr1.prepend(11)
    arr1.insert(1, 23)
    println(arr1)
    arr1.appendAll(arr2)
    println(arr1)
    //4.删除元素
    arr1.remove(3)
    println(arr1)
    arr1 -= 23
    println(arr1)
    //5. 可变数组变成不可变数组
    val arr: ArrayBuffer[Int] = ArrayBuffer(23, 56, 89)
    val newArr: Array[Int] = arr.toArray
    println(newArr.mkString(","))
    println(arr)
    //5. 不可变数组转换为可变数组
    val buffer = newArr.toBuffer
    println(buffer)
    println(newArr)
  }

}
